"""
Case Type   : 小型化
Case Name   : 卸载使用说明
Create At   : 2024/10/31
Owner       : ningyali
Description :
    1.获取安装包并解压
    2.卸载使用说明
    3.清理数据环境
Expect      :
    1.成功
    2.成功
    3.清理数据环境成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.ComInstall import LiteInstall, LiteUnInstall
from testcase.utils.Logger import Logger


class Lite(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.com_install = LiteInstall(self.pkg_path, None, None)
        self.com_uninstall = LiteUnInstall(self.pkg_path)
        self.root_nodes = (Node('PrimaryRoot'),)
        self.user_nodes = (Node('PrimaryDbUser'),)
        self.user = None
        self.port = None
        self.assert_msg = ['--delete-data', '-h|--help']

    def test_upgrade(self):
        text = '----step1:获取数据库安装包并解压;expect:成功----'
        self.log.info(text)
        res = self.com_install.tar_pkg(self.root_nodes[0])
        self.assertIn('install.sh', res, f'执行失败: {text}')

        text = '----step2:卸载使用说明;expect:执行成功----'
        self.log.info(text)
        cmd = f'cd {os.path.join(self.pkg_path, "lite_pkg")};' \
              f'sh uninstall.sh --help'
        self.log.info(cmd)
        res = self.user_nodes[0].sh(cmd).result()
        self.log.info(res)
        for msg in self.assert_msg:
            self.assertIn(msg, res, f'执行失败: {text}')

    def tearDown(self):
        text = '----step3:清理环境;expect:成功----'
        self.log.info(text)
        res = self.com_uninstall.uninstall(*self.user_nodes)
        self.com_uninstall.clear_env(*self.root_nodes)
        self.assertIn('success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
